package com.zp.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.system.entity.SysChooseRoleGroup;
import com.zp.system.entity.SysRoleGroupMenu;
import com.zp.system.entity.SysRoleMenu;
import com.zp.system.entity.bo.SysChooseRoleGroupBo;
import com.zp.system.entity.vo.SysChooseRoleGroupVo;
import com.zp.system.mapper.SysChooseRoleGroupMapper;
import com.zp.system.mapper.SysRoleGroupMenuMapper;
import com.zp.system.mapper.SysRoleMenuMapper;
import com.zp.system.service.ISysChooseRoleGroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * 角色与已选择自定义服务对照Service业务层处理
 *
 * @author zhang peng
 * @date 2022-12-14
 */
@RequiredArgsConstructor
@Service
public class SysChooseRoleGroupServiceImpl implements ISysChooseRoleGroupService {

    private final SysChooseRoleGroupMapper baseMapper;
    private final SysRoleGroupMenuMapper roleGroupMenuMapper;
    private final SysRoleMenuMapper roleMenuMapper;

    /**
     * 查询角色与已选择自定义服务对照
     */
    @Override
    public SysChooseRoleGroupVo queryById (String roleId) {
        return baseMapper.selectVoById(roleId);
    }

    /**
     * 查询角色与已选择自定义服务对照列表(分页)
     */
    @Override
    public TableDataInfo<SysChooseRoleGroupVo> queryPageList (SysChooseRoleGroupBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysChooseRoleGroup> lqw = buildQueryWrapper(bo);
        Page<SysChooseRoleGroupVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询角色与已选择自定义服务对照列表(不分页)
     */
    @Override
    public List<SysChooseRoleGroupVo> queryList (SysChooseRoleGroupBo bo) {
        LambdaQueryWrapper<SysChooseRoleGroup> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysChooseRoleGroup> buildQueryWrapper (SysChooseRoleGroupBo bo) {
        LambdaQueryWrapper<SysChooseRoleGroup> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getRoleId()), SysChooseRoleGroup::getRoleId, bo.getRoleId());
        lqw.eq(StringUtils.isNotBlank(bo.getRoleGroupId()), SysChooseRoleGroup::getRoleGroupId, bo.getRoleGroupId());
        return lqw;
    }

    /**
     * 新增角色与已选择自定义服务对照
     */
    @Override
    public Boolean insertByBo (SysChooseRoleGroupBo bo) {
        SysChooseRoleGroup add = BeanUtil.toBean(bo, SysChooseRoleGroup.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRoleId(add.getRoleId());
        }
        return flag;
    }

    /**
     * 修改角色与已选择自定义服务对照
     */
    @Override
    public Boolean updateByBo (SysChooseRoleGroupBo bo) {
        SysChooseRoleGroup update = BeanUtil.toBean(bo, SysChooseRoleGroup.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (SysChooseRoleGroup entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除角色与已选择自定义服务对照
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 角色与已选择自定义服务对照业务对象
     *
     * @param bo 自定义服务数组
     * @return
     */
    @Override
    public Boolean saveBatch (SysChooseRoleGroupBo bo) {
        List<SysChooseRoleGroup> list = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(Arrays.asList(bo.getRoleGroupIds()))) {
            for (String roleGroupId : bo.getRoleGroupIds()) {
                SysChooseRoleGroup chooseRoleGroup = new SysChooseRoleGroup();
                chooseRoleGroup.setRoleGroupId(roleGroupId);
                chooseRoleGroup.setRoleId(bo.getRoleId());
                // 获取自定义服务所有菜单
                List<SysRoleGroupMenu> roleGroupMenuList = roleGroupMenuMapper.selectList(Wrappers.<SysRoleGroupMenu>lambdaQuery()
                        .eq(SysRoleGroupMenu::getRoleGroupId, roleGroupId));
                if (CollectionUtil.isNotEmpty(roleGroupMenuList)) {
                    List<SysRoleMenu> sysRoleMenuList = new ArrayList<>();
                    roleGroupMenuList.forEach(item -> {
                        SysRoleMenu sysRoleMenu = new SysRoleMenu();
                        sysRoleMenu.setRoleId(bo.getRoleId());
                        sysRoleMenu.setMenuId(item.getMenuId());
                        // 设置服务id
                        sysRoleMenu.setRoleGroupId(roleGroupId);
                        sysRoleMenuList.add(sysRoleMenu);
                    });
                    roleMenuMapper.insertBatch(sysRoleMenuList);
                }
                list.add(chooseRoleGroup);
            }
        }
        return baseMapper.insertBatch(list);
    }

    /**
     * 删除已选择服务
     *
     * @param bo
     * @return
     */
    @Override
    public int delChooseRoleGroup (SysChooseRoleGroupBo bo) {
        // 删除服务
        baseMapper.delete(Wrappers.<SysChooseRoleGroup>lambdaQuery()
                .eq(SysChooseRoleGroup::getRoleGroupId, bo.getRoleGroupId())
                .eq(SysChooseRoleGroup::getRoleId, bo.getRoleId())
        );
        // 删除服务权限
        roleMenuMapper.delete(Wrappers.<SysRoleMenu>lambdaQuery()
                .eq(SysRoleMenu::getRoleGroupId, bo.getRoleGroupId())
                .eq(SysRoleMenu::getRoleId, bo.getRoleId())
        );
        return 1;
    }
}
